package com.bhs.sansonglogistics.ui.home;

import android.content.Intent;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.AdvertisingBean;
import com.bhs.sansonglogistics.bean.MenuBean;
import com.bhs.sansonglogistics.bean.NewNotice;
import com.bhs.sansonglogistics.bean.SlideshowBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.AnnouncementAdapter;
import com.bhs.sansonglogistics.ui.adapter.MenuAdapter;
import com.bhs.sansonglogistics.ui.enterprise.BusinessAdministrationActivity;
import com.bhs.sansonglogistics.ui.enterprise.EnterpriseCollaborationActivity;
import com.bhs.sansonglogistics.ui.order.ScanCodeDeliveryActivity;
import com.bhs.sansonglogistics.ui.order.ShipmentsActivity;
import com.bhs.sansonglogistics.ui.orderDispose.AbnormalOrderActivity;
import com.bhs.sansonglogistics.ui.other.DataBoardActivity;
import com.bhs.sansonglogistics.ui.waybill.LoadingRecordActivity;
import com.bhs.sansonglogistics.ui.waybill.TruckPositioningActivity;
import com.bhs.sansonglogistics.ui.waybill.UpstreamShipmentActivity;
import com.bhs.sansonglogistics.ui.waybill.WaybillSearchActivity;
import com.bhs.sansonglogistics.utils.FastCilckOneUtil;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.view.banner.AutoScrollViewPager;
import com.bhs.sansonglogistics.view.banner.BannerIndicator;
import com.bhs.sansonglogistics.view.banner.ViewPagerAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 伞送
 */
public class SanSongFragment extends BaseFragment implements NetCallBack {

    private AutoScrollViewPager mVpCarousel;
    private BannerIndicator mIndicator;
    private RecyclerView mRvMenu;
    private RecyclerView mRvAnnouncement;
    private List<SlideshowBean> bannerList = new ArrayList<>();
    private RelativeLayout mRlCarousel;


    public static SanSongFragment newInstance() {
        return new SanSongFragment();
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_san_song;
    }

    @Override
    public void initView(View root) {
        mVpCarousel = root.findViewById(R.id.vp_carousel);
        mIndicator = root.findViewById(R.id.indicator);
        mRvAnnouncement = root.findViewById(R.id.rv_announcement);
        mRlCarousel = root.findViewById(R.id.rl_carousel);
        mRlCarousel.post(new Runnable() {
            @Override
            public void run() {
                LogUtils.d(mRlCarousel.getWidth()+"PPP"+mRlCarousel.getHeight());
            }
        });
    }

    @Override
    public void initData() {


        AnnouncementAdapter announcementAdapter = new AnnouncementAdapter(R.layout.item_announcement);
        mRvAnnouncement.setAdapter(announcementAdapter);
        mRvAnnouncement.setLayoutManager(new LinearLayoutManager(getContext()));
        announcementAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty2, null));
        Map<String, String> map = new ArrayMap<>();
        map.put("platform", "logistics");
        map.put("city", "1");
        map.put("ad_sign", "AppLogisticsDesk");
        networkRequest(netApi.get_ad_list(map), this, 9527);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mVpCarousel != null) {
            mVpCarousel.startAutoPlay();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mVpCarousel != null) {
            mVpCarousel.stopAutoPlay();
        }
    }

    private List<MenuBean> menuBeanList = new ArrayList<>();
    private MenuAdapter menuAdapter;

    @Override
    public void prestrain() {
        super.prestrain();
        EventBus.getDefault().register(this);
        mRvMenu = view.findViewById(R.id.rv_menu);

        menuBeanList.add(new MenuBean("装车发货", R.mipmap.ic_vehicle));
        menuBeanList.add(new MenuBean("装车记录", R.mipmap.ic_cord));
        menuBeanList.add(new MenuBean("异常订单", R.mipmap.ic_order_form));
        menuBeanList.add(new MenuBean("企业管理", R.mipmap.ic_manager));

        menuBeanList.add(new MenuBean("上游来货", R.mipmap.ic_upstream_vendor));
        menuBeanList.add(new MenuBean("企业协作", R.mipmap.ic_cooperation));
        menuBeanList.add(new MenuBean("", 0));
        menuBeanList.add(new MenuBean("", 0));

        menuBeanList.add(new MenuBean("发货/取货", R.mipmap.ic_goods));
        menuBeanList.add(new MenuBean("运单查询", R.mipmap.ic_waybill_search));
        menuBeanList.add(new MenuBean("货车定位", R.mipmap.ic_trucks_location));
        menuBeanList.add(new MenuBean("数据看板", R.mipmap.ic_borad));
        menuAdapter = new MenuAdapter(R.layout.item_menu, menuBeanList);
        mRvMenu.setAdapter(menuAdapter);
        mRvMenu.setLayoutManager(new GridLayoutManager(activity, 4));
        menuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (FastCilckOneUtil.isFastClick()) {
                    return;
                }
                switch (menuBeanList.get(position).getMenuName()) {
                    case "运单查询":
                        startActivity(new Intent(getActivity(), WaybillSearchActivity.class));
                        break;
                    case "发货/取货":
                        int logistics_id = SharedPreferencesUtils.getInt(getContext(), "logistics_id", 0);
                        String url = "https://saasdemo.sansongkeji.com/order?logistics_id=" + logistics_id;
                        startActivity(new Intent(getActivity(), ScanCodeDeliveryActivity.class)
                                .putExtra("type", 1)
                                .putExtra("url", url));
                        break;
                    case "装车发货":
                        startActivity(new Intent(getActivity(), ShipmentsActivity.class).putExtra("type", 2));
                        break;
                    case "企业管理":
                        startActivity(new Intent(getActivity(), BusinessAdministrationActivity.class));
                        break;
                    case "上游来货":
                        startActivity(new Intent(getActivity(), UpstreamShipmentActivity.class));
                        break;
                    case "装车记录":
                        startActivity(new Intent(getActivity(), LoadingRecordActivity.class));
                        break;
                    case "企业协作":
                        startActivity(new Intent(getActivity(), EnterpriseCollaborationActivity.class));
                        break;
                    case "异常订单":
                        menuBeanList.get(2).setShow(false);
                        menuAdapter.notifyItemChanged(2);
                        startActivity(new Intent(getActivity(), AbnormalOrderActivity.class));
                        break;
                    case "货车定位":
                        startActivity(new Intent(getActivity(), TruckPositioningActivity.class));
                        break;
                    case "数据看板":
                        startActivity(new Intent(getActivity(), DataBoardActivity.class));
                        // MyUtils.weChatMiniProgram(getContext());
                        break;
                }
            }
        });
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        AdvertisingBean infoBean = new Gson().fromJson(json, AdvertisingBean.class);
        if (infoBean.isStatus() && !MyUtils.isEmpty(infoBean.getData().getList())) {
            ViewPagerAdapter pagerAdapter = new ViewPagerAdapter(infoBean.getData().getList(), getActivity(),true);
            mVpCarousel.setAdapter(pagerAdapter);
            mIndicator.setUpWidthViewPager(mVpCarousel, infoBean.getData().getList().size());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(NewNotice event) {
        if (event.getAboutTable().equals("deliver_order_abnormal")) {
            menuBeanList.get(2).setShow(true);
            menuAdapter.notifyItemChanged(2);
        }
    }
}